package com.harboursoftware.xstorage.results;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.RandomStringUtils;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.harboursoftware.xstorage.util.DateUtils;

/**
 * This class reprsent the result of opration.
 * 
 * @author Simon Leung
 * @since 0.1
 */
public abstract class Result {
    protected ListMultimap<String, String> headers = ArrayListMultimap.create();
    protected int status;
    protected InputStream inputStream;
    /** len == 64 */
    protected String xAmzRequestId;
    /** len == 16 */
    protected String xAmzId2;

    public Result() {
        status = 200;
        xAmzRequestId = RandomStringUtils.randomAlphanumeric(16);
        xAmzId2 = RandomStringUtils.randomAlphanumeric(64);
        setHeader("Server", "X-Storage");
        setHeader("Date", DateUtils.formatDate(new Date()));
        // TODO:是否应该找出这两个参数的意义和作用，requestId是否和应该在请求开始的时候记录下，是不是应该在线程变量里面，出现问题的时候记入日志，
        // x-amz-id2是hostid，这个是不是可以标示不同的分布机器
        setHeader("x-amz-request-id", xAmzRequestId);
        setHeader("x-amz-id-2", xAmzId2);
    }

    public InputStream getInputStream() throws IOException {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    /**
     * 添加一个header，如果原本就存在同名的Header，保留原有的，然后在添加多一个同名的header。
     * 
     * @param name
     * @param value
     */
    public void addHeader(String name, String value) {
        headers.put(name, value);
    }

    // public void addHeaders(String name, String[] values) {
    // String[] oldValues = headers.get(name);
    // if (oldValues == null) {
    // oldValues = values;
    // } else {
    // oldValues = (String[]) ArrayUtils.addAll(oldValues, values);
    // }
    // headers.put(name, oldValues);
    // }
    //    
    public void addAllHeaders(ListMultimap<String, String> headers) {
        headers.putAll(headers);
    }

    /**
     * 设置一个Header,如果原本就存在该Header，覆盖原来的。
     * 
     * @param name
     * @param value
     */
    public void setHeader(String name, String value) {
        headers.removeAll(name);
        headers.put(name, value);
    }

    public void setHeaderContentLength(long length) {
        setHeader("Content-Length", String.valueOf(length));
    }

    public void setHeaderContentTypt(String type) {
        setHeader("Content-Type", type);
    }

    public ListMultimap<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(ListMultimap<String, String> headers) {
        this.headers = headers;
    }

    /**
     * get the status code(the same as HTTP status code) of this operation(default is 200).
     * <p>
     * for more details,refer to the static fields of <a
     * href="http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/http/HttpServletResponse.html"
     * >HttpServletResponse</a> and <a href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/API/">Amazon S3 Error
     * Responses</a>
     * </p>
     * 
     * @return
     */
    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getxAmzRequestId() {
        return xAmzRequestId;
    }

    public String getxAmzId2() {
        return xAmzId2;
    }
}
